package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.property.Property;

//@Cmd(code = Protocol.S_UNION_CONSTRUCT_ACTIVE_REWARD, desc = "领取建设活跃度奖励")
public class GetConstructActiveRewardCmd extends AbstractCommand {

	private int constructActive;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionConstructActiveRewardReqMsg reqMsg = UnionProto.UnionConstructActiveRewardReqMsg.parseFrom(packet.getBytes());
		constructActive = reqMsg.getConstructActive();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		String unionUid = player.getUserInfo().getUnionUid();
		UnionInfo unionInfo = UnionMgr.getUnionInfo(unionUid);
		if(null == unionInfo){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionMember unionMember = UnionMgr.getUnionMember(unionUid, player.getUserId());
		if(null == unionMember){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		int index = ConfigMgr.getUnionConstructActiveIndex(constructActive);
		if (-1 == index) {
			ret = GameErrorCode.E_UNION_CONSTRUCT_ACTIVE_NO_MATCH;
			return;
		}
		ret = UnionMgr.isCanRewardConstruct(unionInfo,unionMember,constructActive);
		if(ret != 0){
			return;
		}
		unionMember.addConstructRewardIndex(constructActive);
		Property reward = ConfigMgr.getUnionConstructActiveReward(index);
		if (null != reward) {
			player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Union, eLogMoneyType.UnionConstructActive);
		}
		player.sendPacket(Protocol.U_UNION_DETAIL, UnionMgr.getUnionDetailMsg(unionUid, eUnionSyncType.Normal.getValue()));
	}

	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionConstructActiveRewardRespMsg.Builder respMsg = UnionProto.UnionConstructActiveRewardRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_UNION_CONSTRUCT_ACTIVE_REWARD, respMsg);
	}

}
